//
//  MessageView.m
//  FaceBoardDome
//
//  Created by kangle1208 on 13-12-12.
//  Copyright (c) 2013年 Blue. All rights reserved.
//

#import "MessageView.h"

#import "FaceBoard.h"


#define FACE_ICON_NAME      @"^[0][0-8][0-5]$"


@implementation MessageView

@synthesize data;
@synthesize font;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        font = [UIFont systemFontOfSize: 16.0f];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        font = [UIFont systemFontOfSize: 16.0f];
    }
    return self;
}

- (void)showMessage:(NSArray *)message {
    
    self.data = (NSMutableArray *)message;

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {

	if ( data ) {
        if (_fontColor) {
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(context, _fontColor.CGColor);
        }
        
        isLineReturn = NO;

        upX = VIEW_LEFT;
        upY = VIEW_TOP;

		for (int index = 0; index < [data count]; index++) {

			NSString *str = [data objectAtIndex:index];
			if ( [str hasPrefix:FACE_NAME_HEAD] ) {

				NSString *imageName = [str substringWithRange:NSMakeRange(1, str.length - 2)];
                UIImage *image = [UIImage imageNamed:imageName];

                if ( image ) {
                    if ( upX > ( rect.size.width - 20 ) ) {

                        isLineReturn = YES;

                        upX = VIEW_LEFT;
                        upY += (VIEW_LINE_HEIGHT - 5);
                        if (upY > self.frame.size.height) {
                            return;
                        }
                    }

                    [image drawInRect:CGRectMake(upX, upY, KFacialSizeWidth, KFacialSizeHeight)];

                    upX += KFacialSizeWidth;

                    lastPlusSize = KFacialSizeWidth;
                }
                else {

                    if (![self drawText:str]) {
                        return;
                    }
                }
			}
            else {
                if (![self drawText:str]) {
                    return;
                }
			}
        }
	}
}

- (BOOL)drawText:(NSString *)string {
    UIFont *textFont = [UIFont systemFontOfSize:16.f];
    for ( int index = 0; index < string.length; index++) {

        NSString *character = [string substringWithRange:NSMakeRange( index, 1 )];

        CGSize size = [character sizeWithFont:textFont
                            constrainedToSize:CGSizeMake(self.frame.size.width, VIEW_LINE_HEIGHT)];
        
        if (upY + VIEW_LINE_HEIGHT > self.frame.size.height && upX > (self.frame.size.width - 31)) {
            character = @"...";
        }
        
        if ( upX > (self.frame.size.width -16)) {

            isLineReturn = YES;

            upX = VIEW_LEFT;
            upY += (VIEW_LINE_HEIGHT - 5);
            
            if (upY > self.frame.size.height) {
                return NO;
            }
        }
        
        [character drawInRect:CGRectMake(upX, upY, size.width, self.bounds.size.height) withFont:textFont];
        upX += size.width;
        lastPlusSize = size.width;
    }
    return YES;
}

/**
 * 判断字符串是否有效
 */
- (BOOL)isStrValid:(NSString *)srcStr forRule:(NSString *)ruleStr {
    
    NSRegularExpression *regularExpression = [[NSRegularExpression alloc] initWithPattern:ruleStr
                                                                                  options:NSRegularExpressionCaseInsensitive
                                                                                    error:nil];
    
    NSUInteger numberOfMatch = [regularExpression numberOfMatchesInString:srcStr
                                                                  options:NSMatchingReportProgress
                                                                    range:NSMakeRange(0, srcStr.length)];
    
    return ( numberOfMatch > 0 );
}



@end
